﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls.Documents;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace AvaloniaApplication1.ViewModels;

public class Person
{
    public string Name { get; set; }
}

public partial class MainWindowViewModel : ViewModelBase
{
#pragma warning disable CA1822 // Mark members as static
    public string Greeting => "Welcome to Avalonia!";
#pragma warning restore CA1822 // Mark members as static

    [ObservableProperty]
    private ObservableCollection<string> _items;

    public MainWindowViewModel()
    {
        Items = new ObservableCollection<string>();
        Items.CollectionChanged += ItemsOnCollectionChanged; 
    }

    private void ItemsOnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            Console.WriteLine("添加");
        }else if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            Console.WriteLine("删除");
        }
    }

    private bool run = true;

    [RelayCommand]
    public void ChooseItem(string name)
    {
        Console.WriteLine(name);
    }
    
    [RelayCommand]
    public async Task AddItems()
    {
        // var temp = Enumerable.Range(1, 100).Select(c => $"{c}").ToList();
        // for (int i = 0; i < temp.Count; i++)
        // {
        //     Items.Add(temp[i]);
        //     await Task.Delay(10);
        // }
        // Items.Add("1");
        // return;
        
        run = true;
        _=Task.Run(async() =>
        {
            while (run)
            {
                await Dispatcher.UIThread.InvokeAsync(async() =>
                {
                    var temp = Enumerable.Range(1, 100).Select(c =>$"{c}").ToList();
                    for (int i = 0; i < temp.Count; i++)
                    {
                        Items.Add(temp[i]);
                        await Task.Delay(10);
                    }
                });
                await Task.Delay(1000);
                await Dispatcher.UIThread.InvokeAsync(async() =>
                {
                    while (Items.Any())
                    {
                        Items.RemoveAt(0);
                        await Task.Delay(10);
                    }
                });
            }
            
        });
    }

    [RelayCommand]
    public void RemoveItems()
    {
        run = false;
        Items.Clear();
        //Items = new ObservableCollection<Person>();
    }
    
    [ObservableProperty]
    private IList<string> _menus;

    public void ChangeMenus()
    {
        var n = new Random().Next(1, 10);
     Menus = Enumerable.Range(1, n).Select(c => $"{c}").ToList();
    }

    [RelayCommand]
    public void ChooseMenu(string item)
    {
        Console.WriteLine(item);
    }
}